package com.ziytek.web.citizen.service;

import com.ziytek.web.citizen.config.ConfigInit;
import com.ziytek.web.citizen.constant.SettingConstant;
import com.ziytek.web.citizen.mapper.BasicCabinetIpMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/***
 * 设备离线
 * @author fenghx
 * */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeviceOfflineService {
    private final ConfigInit configInit;
    private final BasicCabinetIpMapper cabinetIpMapper;
    private final InterruptIssuedService interruptIssuedService;


    public void deviceOffline(String clientIp) {
        Assert.notNull(clientIp, "clientIp不能为空");

        log.info("设备离线clientIp：{}", clientIp);

        var ips = cabinetIpMapper.queryCabinetIpInfoByIp(clientIp);
        if (null == ips || !SettingConstant.IS_AVAILABLE.equalsIgnoreCase(ips.getAvailable()) || StringUtils.isBlank(ips.getCabinetNo())) {
            return;
        }

        var cab = String.valueOf(Integer.parseInt(ips.getCabinetNo()));
        if (configInit.isInterruptAutomatic()) {
            interruptIssuedService.interruptIssued(cab);
        }
    }
}
